<?php
/**
 * Created by PhpStorm.
 * 商品分类
 */

namespace App\frontend\controllers\goods;

use App\common\components\BaseController;
use App\common\models\Category;

class CategoryController extends BaseController
{
    public function getLists()
    {
        $data = [
            ["id" => 22, "name" => "123"],
            ["id" => 15, "name" => "茶", "lists" => [["id" => 118, "name" => "奶茶"], ["id" => 127, "name" => "柠檬茶"]]],
            ["id" => 10, "name" => "蔬菜"],
            ["id" => 13, "name" => "水果", "lists" => [["id" => 14, "name" => "草莓"], ["id" => 127, "name" => "香蕉"], ["id" => 117, "name" => "苹果"]]],
            ["id" => 7, "name" => "饮料", "lists" => [["id" => 18, "name" => "奶茶"], ["id" => 17, "name" => "柠檬茶"]]],
        ];

        return successJson('ok!', $data);
    }

    /**
     * 获取商品分类
     * $pid  int   默认0
     * @desc 根据parent_id获取数据
     */
    public function getCategoryList()
    {

        $pageSize = 100;

        $pid = intval(request()->input('pid')) ?: '0';

        $list = Category::getCategorys($pid)->where('enabled', 1)->paginate($pageSize)->toArray();

        if (!$list['data']) {
            return errorJson('未检测到分类数据!');
        }

        //格式化图片
        foreach ($list['data'] as &$item) {
            $item->thumb = tomedia($item->thumb);
            $item->adv_img = tomedia($item->adv_img);
        }
        return successJson('获取分类数据成功!', $list);
    }


}
